//
//  ZPMineSetViewController.m
//  ZPIOSEducation
//
//  Created by 李伟 on 2016/12/3.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPMineSetViewController.h"
#import "ZPMineAboutViewController.h"

#define zClearCacheActionSheetTag    1000
#define zLogoutActionSheetTag        1001


@interface ZPMineSetViewController ()
{
    NSArray  *headerTitles;
    NSString *cacheText;
    NSString *versionString;
    UIView   *clearView;
}

@property (weak, nonatomic) IBOutlet UITableView *setTableView;
@property (weak, nonatomic) IBOutlet UIButton    * logoutBtn;

@end

static NSString *const reuseIdentifier = @"ZPSetTabViewCell";

@implementation ZPMineSetViewController


- (ZPSetTabViewCell *)cell
{
    if (_cell == nil) {
        _cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([ZPSetTabViewCell class]) owner:self options:nil] lastObject];
    }
    return _cell;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    headerTitles = @[@"清除本地缓存",@"给我们评分鼓励下吧",@"版本号"];//@"推荐石头培优给好友"
    self.setTableView.delegate = self;
    self.setTableView.dataSource = self;
    
    // 缓存
    NSInteger cacheSize = [[SDImageCache sharedImageCache] getSize];
    cacheText = [NSString stringWithFormat:@"%.2fMB",cacheSize/1024.0/1024.0];
    
    
    // 版本号
    versionString = [NSString  stringWithFormat:@"v%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    
    
    // 是否登录
    if ([self isLogin]) {
        self.logoutBtn.hidden = NO;
    } else {
        self.logoutBtn.hidden = YES;
    }
}


- (BOOL)isLogin
{
    NSMutableDictionary *dataDic = [[ZPAuthStore sharedZPAuthStore] userToDic];
    NSString *token = [dataDic valueForKey:@"token"];
    NSString *memberId = [dataDic valueForKey:@"memberId"];
    if ([memberId isEqual:[NSNull null]] ||[memberId length] <=0 ||[token isEqual:[NSNull null]] ||[token length] <=0) {
        return NO;
    }else {
        return YES;
    }
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.title = @"设置";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhenpin_back"] style:UIBarButtonItemStylePlain target:self action:@selector(clickBackButtonItem)];
}

- (void)clickBackButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)showClearView
{
    if (!clearView) {
        clearView = [[UIView  alloc] initWithFrame:CGRectMake((WIDTH(self.setTableView) - 100) / 2, (HEIGHT(self.setTableView) - 66) / 2, 100, 66)];
        clearView.backgroundColor = zFreeColor(0, 0, 0, 0.65);
        ViewRadius(clearView, 8.);
        [self.setTableView  addSubview:clearView];
        
        
        UIImageView  *imageView = [[UIImageView  alloc] initWithFrame:CGRectMake(40, 12, 20, 20)];
        imageView.image = [UIImage  imageNamed:@"setting_clear"];
        [clearView  addSubview:imageView];
        
        UILabel  *label = [[UILabel  alloc] initWithFrame:CGRectMake(10, 38, 80, 20)];
        [[ZPShareView sharedSingleView] setupLabel:label masksToBounds:NO cornerRadius:0 backgroundColor:clear_color text:@"清除成功" textColor:white_color font:14. textAlignment:NSTextAlignmentCenter];
        [clearView  addSubview:label];
        cacheText = @"0.00MB";
        [self.setTableView reloadData];
        
    }
    clearView.hidden = NO;
}


- (void)hiddenClearView
{
    if (clearView) {
        clearView.hidden = YES;
    }
}





- (IBAction)logut:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"确认要退出登录吗?"
                                                            delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"退出" otherButtonTitles: nil];
    actionSheet.tag = zLogoutActionSheetTag;
    [actionSheet showInView:self.view];

}


#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return headerTitles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    self.cell.headerTitle.text = headerTitles[indexPath.row];
    
    if (indexPath.row == 0)
    {
        self.cell.detailedTitle.text = cacheText;
    }
    else if (indexPath.row == 2)
    {
        self.cell.detailedTitle.text = versionString;
        self.cell.detailedTitle.textColor = [UIColor colorWithRed:5.0/255 green:5.0/255 blue:4.0/255 alpha:1];
        self.cell.cellArrow.hidden = YES;
    }
    else
    {
        [self.cell.detailedTitle setHidden:YES];
    }
    return self.cell;
}



#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSInteger  section = [indexPath  section];
    NSInteger  row = [indexPath  row];
    if (section == 0) {
        if (row == 0) {
            // 清除本地缓存
            UIActionSheet  *actionSheet = [[UIActionSheet  alloc] initWithTitle:@"清除后，图片等信息需重新下载，确认清除?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"清除" otherButtonTitles: nil];
            actionSheet.tag = zClearCacheActionSheetTag;
            [actionSheet  showInView:self.view];
        } else if (row == 1) {
            
            // 给我们评分鼓励下吧
            NSString *appID = @"1247755439";
            NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
            [[UIApplication sharedApplication] openURL: [NSURL URLWithString:str]];

        }
//        else if (row == 2) {
//            // 关于石头培优
//            ZPMineAboutViewController *aboutVC = [[ZPMineAboutViewController alloc] initWithNibName:NSStringFromClass([ZPMineAboutViewController class]) bundle:nil];
//            [self.navigationController pushViewController:aboutVC animated:YES];
//            // 推荐石头培优给好友
//          //  [self  showShareView];
//        } 
    }
}



#pragma mark - <展示分享视图>
- (void)showShareView
{

}



#pragma mark - <清除缓存>
- (void)cleanCache
{
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil]; // 更新前清除缓存方法 clearDisk
    [self.setTableView reloadData]; // 刷新数据
    [self showClearView];  // 展示缓存已清除
    [self performSelector:@selector(hiddenClearView) withObject:nil afterDelay:1.]; // 单线程方法,只有当前调用此方法的函数执行完毕后，selector方法才会被调用
}



#pragma mark - <UIActionSheetDelegate>
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    switch (actionSheet.tag) {
        case zClearCacheActionSheetTag:
            if (buttonIndex == 0) { // 点击清除缓存
                [self cleanCache];
            } break;
            case zLogoutActionSheetTag:
            if (buttonIndex == 0) { // 点击退出登录
               //  [[XNSDKCore sharedInstance]logout]; // 珍品的客服退出
                [[ZPAuthStore sharedZPAuthStore] clear];
                
                [[ZPNetworking  sharedZPNetworking] GET:APILoginOut parameters:nil progress:^(NSProgress * _Nullable progress) {
                    
                } success:^(id  _Nonnull responseObject) {
                    
                } failure:^(NSError * _Nonnull error) {
                    
                }];
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"logOutNotification" object:nil];
                [self.navigationController popToRootViewControllerAnimated:YES];
            } break;
        default:
            break;
    }
}
@end
